package com.guli.gulimall.product.web;

import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;
import com.guli.gulimall.product.service.SkuInfoService;
import com.guli.gulimall.product.service.SpuInfoService;
import com.guli.gulimall.product.vo.CategoryLevel2Vo;
import com.guli.gulimall.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author WZH
 * @title: IndexController
 * @projectName gulimall-java
 * @description:
 * @date 2021/12/23
 */
@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SkuInfoService skuInfoService;

    @RequestMapping(value = {"/","/index"})
    public String index(Model model){
        //先回显一级树
        List<CategoryEntity> list=categoryService.selectL1List();
        model.addAttribute("list",list);
        return "index";
    }

    /**
     * @Author WZH
     * @方法描述:查询三级树的后两层给catalogLoader.js
     * @Create: 2021/12/23
     */
    @RequestMapping(value = {"/findCatalog"})
    @ResponseBody
    public Map<String,List<CategoryLevel2Vo>> findCatalog(){
        return categoryService.findCatalogRedissonLockCache();
    }

    /**
     * @Author WZH
     * @方法描述:
     * @Param: 商品详情
     * @Return:
     * @Create: 2022/1/12
     */
    @RequestMapping(value = {"/{skuId}.html"})
    public String toDesc(@PathVariable("skuId") Integer skuId, Model model){
        SkuItemVo skuInfo=skuInfoService.getItemVoBySkuId(skuId);
        model.addAttribute("skuInfo",skuInfo);
        return "item";
    }
}
